package com.lkm.market2_0.entity;



import com.google.gson.Gson;
import com.google.gson.GsonBuilder;
import com.google.gson.JsonElement;
import com.google.gson.JsonObject;
import com.google.gson.JsonParser;
import com.lkm.frame.P;
import com.lkm.frame.entity.ResponEntityAbs;

import java.lang.reflect.Type;



public class ResponEntity<Data> extends ResponEntityAbs<Data> {

	public ResponEntity(){
		super();
	}
	public ResponEntity(ResponEntityAbs<?> r) {
		super(r);
	}

	public static final int Code_Autho_Fail = 2;
	public static final int Code_Success = 1;
	public static final int Code_Fail_TipsMsg = 0;
	
	
	public static final int Code_ExitLogin = -2;
	
	public static final int Code_Fail_NotTipsMsg = 10000;
	
	public static final int Code_NoData = 333333;
	public static final int Code_Cancel = -2;
	public static final int Code_NotResponse = -3;
	public static final int Code_Null = Code_NotResponse;

	@Override
	public boolean isNotSrcData() {
		return getCode() == Code_NotResponse;
	}

	

	public ResponEntity<Data> successNotMsg(){
		if( getCode() == Code_Success )
			setMsg("");
		return this;
	}

	public static <Data> ResponEntity<Data> success(Data data) {
		return new ResponEntity().success(data, null);

	}

	public static <Data> ResponEntity<Data> Cancel() {
		ResponEntity<Data> re = new ResponEntity<Data>();
		re.setCode(Code_Cancel);
		re.setMsg("");
		return re;

	}

	public static <Data> ResponEntity<Data> Fail() {
		return Fail(null);

	}

	public static <Data> ResponEntity<Data> Fail(String msg) {

		ResponEntity re = new ResponEntity<Data>();
		re.fail(msg == null ? re.getMsg() : msg);

		return re;

	}

	public static <Data> ResponEntity<Data> Fail_NotResponse() {

		ResponEntity<Data> re = new ResponEntity<Data>();
		re.setMsg("数据请求失败");
		re.setCode(Code_NotResponse);

		return re;

	}

	public static <Data> ResponEntity<Data> fromJson(String json, Type c,String apiInfo) {
		return fromJson(json, c, Code_Success,apiInfo);
	}



//			java.lang.reflect.Type c) {


//		return fromJson(mResult.content, c,mResult.apiInfo);


	

	public static <Data> ResponEntity<Data> fromJson(String json,

			Type c, int successCode,String apiInfo) {

//		System.out.println("====== " + json);

		if (json == null)
			return Fail_NotResponse();

		ResponEntity<Data> result = null;
		try {
			JsonParser jp = new JsonParser();
			JsonObject js = (JsonObject) jp.parse(json);
			result = new ResponEntity<Data>();

			JsonElement stateJe = js.get("res");
			if (stateJe != null) {
				int i = stateJe.getAsInt();
				result.setCode(i == successCode ? Code_Success : i);
			}

			JsonElement msgJe = js.get("msg");
			try {
				if (msgJe != null) {
					result.setMsg(msgJe.getAsString());
				} else {
					result.setMsg(result.isSuccess() ? "" : "数据解析失败");
				}
			} catch (Exception e) {
				result.setMsg("");
			}

			if (c != null) {
				JsonElement dataje = js.get("data");
				try {
					if (dataje != null){

//							result.setData((Data) newGson().fromJson(dataje, c));

//							result.setData((Data) newGson().fromJson(dataje.getAsString(), c));

						
						try{
							result.setData((Data) newGson().fromJson(dataje, c));
						}catch(Exception e){
							if(result.isSuccess())
							e.printStackTrace();
							if(c == String.class){
								result.setData((Data) dataje.toString());
							}else
							
							result.setData((Data) newGson().fromJson(dataje.getAsString(), c));
						}
						
					}
					
				} catch (Exception e) {
					if(result.isSuccess())
					e.printStackTrace();
					
					// if(dataje.getAsString().equalsIgnoreCase("null"))
					
					
					
					
				}
				if (result.isSuccess() && result.getCode() != ResponEntity.Code_Null  && result.getData() == null)
					throw new NullPointerException();
			}
		} catch (Exception e) {
			e.printStackTrace();
			result = Fail("服务器繁忙");
//			UpDataResolveFailTask.UpDataResolveFail(MyApplicationBase.application, apiInfo,json);
		}
		return result;

	}

	public static Gson newGson() {
		GsonBuilder b = new GsonBuilder();

//		b.registerTypeAdapter(Boolean.class, serializer);
//		b.registerTypeAdapter(boolean.class, serializer);

//		b.registerTypeAdapter(CharSequence.class, new CharSequenceSerializer());
		
		b.serializeNulls();
		Gson gson = b.create();
		return gson;
	}
	public static String toJson(Object o){
		return newGson().toJson(o);
	}
	public static <T> T fromJsonstr(String json,Type classOfT){
		return newGson().fromJson(json, classOfT);
	}

	

	

	

	

	

	
	
	
	
	
	
	

	
	
	
	
	
	

	





//				Toast.makeText(context, getMsg(), Toast.LENGTH_SHORT).show();





	

	public boolean isNotData() {
		return getCode() == ResponEntity.Code_Null || getData() == null;
	}






	@Override
	public boolean isSuccess() {
		return getCode() == Code_Success || getCode() == Code_NoData;
	}

	

	public boolean isAuthoFail() {
		return getCode() == Code_Autho_Fail;
	}


	public boolean hasNextPage() {
		return getCode() != ResponEntity.Code_NoData;
	}
	@Override
	public ResponEntity<Data> success(Data data, String msg) {
		setData(data);
		setMsg(msg);
		setCode(Code_Success);
		return this;
	}

	@Override
	public ResponEntityAbs<Data> fail(Data data, String msg) {
		setData(data);
		setMsg(msg);
		setCode(Code_Fail_TipsMsg);
		return this;
	}

	
	public ResponEntityAbs<Data> exitLogin(Data data, String msg) {
		fail(null);
		setCode(ResponEntity.Code_ExitLogin);
		setData(null);
		return this;
	}
	public ResponEntity<Data> fail(String msg) {
		setData(null);
		setMsg(msg);
		setCode(Code_Fail_TipsMsg);
		return this;
	}

	

}
